package homework;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class Response {
    private OutputStream outputStream;

    public Response(){
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void output(String content) throws IOException {
        outputStream.write(content.getBytes(StandardCharsets.UTF_8));
    }

    public void outputHtml(String path) throws IOException {
      String absoluteResourcePath =  StaticResourceUtil.getAbsolutePath(path);
      File file = new File(absoluteResourcePath);

      if(file.exists() && file.isFile()){
          StaticResourceUtil.outputStaticResource(new FileInputStream(file), outputStream);
      } else {
          output(HttpProtocolUtil.getHttpHeader404());
      }
    }

}
